import json
import traceback
import time
import random
import string

import gevent
from flask import current_app


from flask import request


def proc_request_data():
    try:
        # print("data:", request.data)
        req_json = json.loads(request.data)
        print("req_json:", req_json)
        return req_json
    except Exception as e:
        print(traceback.format_exc())

    return {}


def app_sleep(s: float):
    if current_app.use_gevent:
        gevent.sleep(s)
    else:
        time.sleep(s)


def get_random_str(size=10):
    return ''.join([random.choice(string.ascii_letters) for i in range(size)])


def dump_func(*args):
    pass


def safe_json_loads(data):
    try:
        return json.loads(data)
    except Exception as e:
        return {}


def safe_json_dump(data):
    try:
        return json.dumps(data)
    except Exception as e:
        print(traceback.format_exc())
        return None

